﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Storm;
using Storm.Docking;
using Storm.Docking.Controls;
using Storm.Docking.Visual;
using Storm.Docking.Visual.Drawing;
using Storm.Docking.Visual.Glyphs;

namespace Storm.Docking.Controls
{
	/// <summary>
	/// Represents a Panel that contains a DockPanel's Form and DockCaption.
	/// It is contained in a DockPanel, where it is added to the DockPanel's
	/// DockTab Control.
	/// </summary>
	[ToolboxItem(false)]
	public class DockContainer
		: Panel
	{
		#region Fields

		private Form         form        = null;
		private DockPanel    dockPanel   = null;
		private DockCaption  dockCaption = null;
		private DockRenderer renderer    = null;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the parent DockPanel of the DockContainer.
		/// NOTE: does not contain updating logic!
		/// </summary>
		[Browsable(false)]
		[Description("Gets or sets the parent DockPanel of the DockContainer. NOTE: does not contain updating logic!")]
		public DockPanel DockPanel
		{
			get { return dockPanel; }
			set { dockPanel = value; }
		}

		/// <summary>
		/// Gets or sets the DockCaption of the DockContainer.
		/// </summary>
		[Browsable(false)]
		[Description("Gets or sets the DockCaption of the DockContainer.")]
		public DockCaption DockCaption
		{
			get { return dockCaption; }
			set { dockCaption = value; }
		}

		/// <summary>
		/// Gets or sets the Form of the DockContainer.
		/// </summary>
		[Browsable(true)]
		[Description("Gets or sets the Form of the DockContainer.")]
		public Form Form
		{
			get { return form; }
			set
			{
				form = value;
				if (this.Form != null)
				{
					// Setup the Form so we can make it contained in the
					// DockContainer and then dock it in it.

					this.Form.Parent = null;
					this.Form.TopLevel = true;
					this.Form.Owner = this.FindForm();
					this.Form.TopLevel = false;

					this.Form.FormBorderStyle = FormBorderStyle.None;
					this.Form.Dock = DockStyle.Fill;
					this.DockCaption.CaptionText = this.Form.Text;
					this.Text = this.DockCaption.CaptionText;

					this.Form.Parent = this;
					this.Controls.Add(this.Form);

					this.Form.Show();
					this.Form.BringToFront();
					this.AutoScroll = false;
				}
			}
		}

		/// <summary>
		/// Gets or sets the DockRenderer of the DockContainer.
		/// </summary>
		public DockRenderer Renderer
		{
			get { return renderer; }
			set
			{
				renderer = value;

				// Update all the child Controls and their Renderers.

				foreach (Control control in this.Controls)
				{
					if (control is DockCaption)
						(control as DockCaption).Renderer = this.Renderer;
				}

				this.Invalidate();
			}
		}

		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="DockContainer"/> class.
		/// </summary>
		public DockContainer()
		{
			this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
			this.BackColor = Color.Transparent;
		}
	}
}
